package org.jeecg.modules.lab.notify.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lab.device.vo.LabDevicePage;
import org.jeecg.modules.lab.notify.entity.LabNotify;
import org.jeecg.modules.lab.notify.service.ILabNotifyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.notify.vo.LabNotifyPage;
import org.jeecg.modules.lab.notify.vo.LabNotifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 消息通知
 * @Author: jeecg-boot
 * @Date: 2023-11-08
 * @Version: V1.0
 */
@Api(tags = "消息通知")
@RestController
@RequestMapping("/notify/labNotify")
@Slf4j
public class LabNotifyController extends JeecgController<LabNotify, ILabNotifyService> {
    @Autowired
    private ILabNotifyService labNotifyService;

    /**
     * 分页列表查询
     *
     * @param labNotify
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "消息通知-分页列表查询")
    @ApiOperation(value = "消息通知-分页列表查询", notes = "消息通知-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabNotify>> queryPageList(LabNotify labNotify,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<LabNotify> queryWrapper = QueryGenerator.initQueryWrapper(labNotify, req.getParameterMap());
        Page<LabNotify> page = new Page<LabNotify>(pageNo, pageSize);
        IPage<LabNotify> pageList = labNotifyService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    //首页初始10条消息
    @GetMapping(value = "/listOne")
    public Result<IPage<LabNotify>> listOne() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Page<LabNotify> page = new Page<>(1, 10);
        //查询已发布的十条
        labNotifyService.page(page, new LambdaQueryWrapper<LabNotify>()
                .eq(LabNotify::getUserId, loginUser.getId())
                .eq(LabNotify::getNotStatus, "未读")
                .orderByDesc(LabNotify::getPubTime));
        return Result.ok(page);
    }

    //移动端首页初始10条消息
    @GetMapping(value = "/listApp")
    public Result<IPage<LabNotify>> listApp(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Page<LabNotify> page = new Page<>(pageNo, pageSize);
        //查询已发布的十条
        labNotifyService.page(page, new LambdaQueryWrapper<LabNotify>()
                .eq(LabNotify::getUserId, loginUser.getId())
                .orderByDesc(LabNotify::getPubTime));
        return Result.ok(page);
    }


    //移动端首页消息
    @GetMapping(value = "/listAppNotify")
    public Result<IPage<LabNotifyPage>> listAppNotify(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Page<LabNotifyPage> page = new Page<>(pageNo, pageSize);
        List<LabNotifyPage> labNotifyPages = labNotifyService.listAppNotify(loginUser);
        List<LabNotifyPage> notifyPageList = labNotifyPages.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        page.setTotal(labNotifyPages.size());
        page.setRecords(notifyPageList);
        return Result.ok(page);
    }


    //待办任务，当天待办的实验
    @GetMapping(value = "/todo")
    public Result<List<LabNotifyVo>> toDoList(@RequestParam(name = "date") String date) throws ParseException {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<LabNotifyVo> labNotifyVos = labNotifyService.listMonth(date, loginUser.getId());
        return Result.ok(labNotifyVos);
    }

    //所有待办实验
    @GetMapping(value = "/todoAll")
    public Result<List<LabNotify>> todoAll() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<LabNotify> list = labNotifyService.list(new LambdaQueryWrapper<LabNotify>()
                .eq(LabNotify::getUserId, loginUser.getId())
                .and(i -> i.eq(LabNotify::getNotTitlle, "实验").or().eq(LabNotify::getNotTitlle, "实验复核"))
                .orderByDesc(LabNotify::getPubTime));
        return Result.ok(list);
    }

    //点击消息，修改状态为已读
    @GetMapping(value = "/updateStatus")
    public void updateStatus(@RequestParam String id) {
        LabNotify notify = labNotifyService.getById(id);
        notify.setNotStatus("已读");
        labNotifyService.updateById(notify);
    }

    /**
     * 添加
     *
     * @param labNotify
     * @return
     */
    @AutoLog(value = "消息通知-添加")
    @ApiOperation(value = "消息通知-添加", notes = "消息通知-添加")
    @RequiresPermissions("notify:lab_notify:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabNotify labNotify) {
        labNotifyService.save(labNotify);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labNotify
     * @return
     */
    @AutoLog(value = "消息通知-编辑")
    @ApiOperation(value = "消息通知-编辑", notes = "消息通知-编辑")
    @RequiresPermissions("notify:lab_notify:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabNotify labNotify) {
        labNotifyService.updateById(labNotify);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "消息通知-通过id删除")
    @ApiOperation(value = "消息通知-通过id删除", notes = "消息通知-通过id删除")
    @RequiresPermissions("notify:lab_notify:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labNotifyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "消息通知-批量删除")
    @ApiOperation(value = "消息通知-批量删除", notes = "消息通知-批量删除")
    @RequiresPermissions("notify:lab_notify:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labNotifyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "消息通知-通过id查询")
    @ApiOperation(value = "消息通知-通过id查询", notes = "消息通知-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabNotify> queryById(@RequestParam(name = "id", required = true) String id) {
        LabNotify labNotify = labNotifyService.getById(id);
        if (labNotify == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labNotify);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labNotify
     */
    @RequiresPermissions("notify:lab_notify:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabNotify labNotify) {
        return super.exportXls(request, labNotify, LabNotify.class, "消息通知");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("notify:lab_notify:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabNotify.class);
    }

}
